package ren.kane.phoenix.util.io;

import java.io.File;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

/**
 * @author renrk(1275) on 2019-07-31 4:50 PM
 **/
public class FileTest {

    private static void testFileStaticFields() {
        // 打印 路径分隔符":"
        System.out.printf("File.pathSeparator=\"%s\"\n", File.pathSeparator);
        // 打印 路径分隔符':'
        System.out.printf("File.pathSeparatorChar=\"%c\"\n", File.pathSeparatorChar);
        // 打印 分隔符"/"
        System.out.printf("File.separator=\"%s\"\n", File.separator);
        // 打印 分隔符'/'
        System.out.printf("File.separatorChar=\"%c\"\n", File.separatorChar);
    }

    private static void testFileDirAPIS() {
        try {
            File dir = new File("dir");
            dir.mkdir();

            File sub1 = new File("dir", "sub1");
            sub1.mkdir();

            File sub2 = new File(dir, "sub2");
            sub2.mkdir();

            File sub3 = new File("dir/sub3");
            sub3.mkdirs();

            String dirAbsPath = dir.getAbsolutePath();
            String sub4AbsPath = dirAbsPath + File.separator + "sub4";
            File sub4 = new File(sub4AbsPath);
            sub4.mkdirs();

            File normal = new File(dir, "normal.txt");
            normal.createNewFile();

            String absPath = dirAbsPath + File.separator + "abs.txt";
            File abs = new File(absPath);
            abs.createNewFile();

            File subNormal = new File(sub1, "sub_normal.txt");
            subNormal.createNewFile();

            System.out.printf("%30s = %s\n", "subNormal.exists()", subNormal.exists());
            System.out.printf("%30s = %s\n", "subNormal.getPath()", subNormal.getPath());
            System.out.printf("%30s = %s\n", "subNormal.getName()", subNormal.getName());
            System.out.printf("%30s = %s\n", "subNormal.getParent()", subNormal.getParent());
            System.out.printf("%30s = %s\n", "subNormal.getAbsolutePath()", subNormal.getAbsolutePath());
            System.out.printf("%30s = %s\n", "subNormal.getCanonicalPath()", subNormal.getCanonicalPath());
            System.out.printf("%30s = %s is \"%s\"\n", "subNormal.lastModified()", subNormal.lastModified(), getModifyTime(subNormal.lastModified()));
            System.out.printf("%30s = %s\n", "subNormal.toURI()", subNormal.toURI());

            System.out.println("---- list files and folders ----");
            File[] files = dir.listFiles();
            Arrays.stream(files).forEach(f -> System.out.printf("%-30s  %s%s%s%s\n",
                f.getName(), f.isFile() ? "[File]" : "", f.isDirectory() ? "[Directory]" : "", f.isAbsolute() ? "[Absolute]" : "", f.isHidden() ? "[Hidden]" : ""));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String getModifyTime(long millis) {
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneId.systemDefault())
            .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static void main(String[] args) {
        testFileStaticFields();
        testFileDirAPIS();
    }
}
